package com.tj.jinzao.activity;


import android.app.Activity;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;

import com.tj.jinzao.R;
import com.tj.jinzao.base.BaseActivity;
import com.tj.jinzao.base.IBaseView;
import com.tj.jinzao.model.Address;
import com.tj.jinzao.model.ProvinceCity;
import com.tj.jinzao.presenter.AddressPresenter;
import com.tj.jinzao.utils.ToastUtils;
import com.tj.jinzao.widgets.ChooseBottomDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 收货信息页面
 * Created by wjy on 2019/1/5.
 */
public class AddressDetailActivity extends BaseActivity implements IBaseView<List<Address>>, ChooseBottomDialog.OnWheelValueListener {

    @BindView(R.id.et_name)
    EditText mNameView;
    @BindView(R.id.et_phone)
    EditText mPhoneView;
    @BindView(R.id.tv_area)
    TextView mAreaView;
    @BindView(R.id.et_address)
    EditText mAddressView;

    private Address mAddress;

    private ChooseBottomDialog mDialog;

    private List<ProvinceCity> mCityList;

    private int mIndex1, mIndex2, mIndex3;

    private AddressPresenter mPresenter;

    @Override
    protected void setupView() {
        super.setupView();
        mPresenter = new AddressPresenter(this);
        mAddress = (Address) getIntent().getSerializableExtra("address");
        if (mAddress == null) {
            mAddress = new Address();
        } else {
            mNameView.setText(mAddress.getName());
            mAreaView.setText(mAddress.getArea());
            String address = mAddress.getProvince() + "、" + mAddress.getCity() + "、" + mAddress.getArea();
            mAreaView.setText(address);
            mPhoneView.setText(mAddress.getMobile());
            mAddressView.setText(mAddress.getDetail());
        }
        initList();
    }

    private void initList() {
        mCityList = mPresenter.getCityList();
        mIndex1 = getIndex1();
        mIndex2 = getIndex2();
        mIndex3 = getIndex3();
    }

    @OnClick(R.id.rl_city_layout)
    public void chooseCity() {
        mDialog = new ChooseBottomDialog(this);
        mDialog.setValueListener(this);
        mDialog.setData(getProvince(), getCityList(mIndex1), getAreaList(mIndex1, mIndex2)
                , mAddress.getProvince(), mAddress.getCity(), mAddress.getArea());
        mDialog.show();
    }

    private int getIndex1() {
        String province = mAddress.getProvince();
        if (!TextUtils.isEmpty(province)) {
            int index = 0;
            for (ProvinceCity city : mCityList) {
                if (TextUtils.equals(city.getName(), province)) {
                    return index;
                }
                index++;
            }
        }
        return 0;
    }

    private int getIndex2() {
        String city = mAddress.getCity();
        if (!TextUtils.isEmpty(city)) {
            List<ProvinceCity.CityBean> list = mCityList.get(getIndex1()).getCityList();
            int index = 0;
            for (ProvinceCity.CityBean cityBean : list) {
                if (TextUtils.equals(cityBean.getName(), city)) {
                    return index;
                }
                index++;
            }
        }
        return 0;
    }

    private int getIndex3() {
        String area = mAddress.getArea();
        if (!TextUtils.isEmpty(area)) {
            List<String> list = mCityList.get(getIndex1()).getCityList().get(getIndex2()).getArea();
            int index = 0;
            for (String str : list) {
                if (TextUtils.equals(str, area)) {
                    return index;
                }
                index++;
            }
        }
        return 0;
    }

    @Override
    protected CharSequence getRightButtonText() {
        return "保存";
    }

    @Override
    protected void clickRightButton() {
        String name = mNameView.getText().toString().trim();
        if(TextUtils.isEmpty(name)){
            ToastUtils.showToast(this,"请输入收件人");
            return;
        }
        String phone = mPhoneView.getText().toString().trim();
        if(TextUtils.isEmpty(phone)){
            ToastUtils.showToast(this,"请输入手机号");
            return;
        }
        String area = mAreaView.getText().toString().trim();
        if(TextUtils.isEmpty(area)){
            ToastUtils.showToast(this,"请选择城市");
            return;
        }
        String address = mAddressView.getText().toString().trim();
        if(TextUtils.isEmpty(address)){
            ToastUtils.showToast(this,"请输入详细地址");
            return;
        }
        String areas[] = area.split("、");
        String province = areas[0];
        String city = areas[1];
        area = areas[2];
        if (mAddress.getId() != 0) {
            mPresenter.updateAddress(mAddress.getId(), name,phone,province, city,area,address);
        }else {
            mPresenter.addAddress(name,phone,province, city,area,address);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(mPresenter != null) mPresenter.detachView();
    }

    @Override
    public int getLayoutResource() {
        return R.layout.activity_address_detail;
    }

    @Override
    protected int getLeftButtonResId() {
        return R.mipmap.ic_back;
    }

    @Override
    public void onValueChanged(String select1, String select2, String select3) {
        mAddress.setProvince(select1);
        mAddress.setCity(select2);
        mAddress.setArea(select3);
        String address = mAddress.getProvince() + "、" + mAddress.getCity() + "、" + mAddress.getArea();
        mAreaView.setText(address);
    }

    @Override
    public void onValueChanging(int selectIndex, int wheelIndex) {
        if (wheelIndex == 0) {
            if (selectIndex != mIndex1) {
                mDialog.refreshData(1, getCityList(selectIndex), "");
                mDialog.refreshData(2, getAreaList(selectIndex, 0), "");
            }
            mIndex1 = selectIndex;
        } else if (wheelIndex == 1) {
            if (selectIndex != mIndex2) {
                mDialog.refreshData(2, getAreaList(mIndex1, selectIndex), "");
            }
            mIndex2 = selectIndex;
        } else if (wheelIndex == 2) {
            mIndex3 = selectIndex;
        }
    }

    private List<String> getCityList(int index) {
        List<ProvinceCity.CityBean> list = mCityList.get(index).getCityList();
        List<String> strings = new ArrayList();
        for (ProvinceCity.CityBean bean : list) {
            strings.add(bean.getName());
        }
        return strings;

    }

    private List<String> getAreaList(int index, int cityIndex) {
        return mCityList.get(index).getCityList().get(cityIndex).getArea();
    }

    private List<String> getProvince() {
        List<String> strings = new ArrayList();
        for (ProvinceCity bean : mCityList) {
            strings.add(bean.getName());
        }
        return strings;
    }


    @Override
    public Activity getContext() {
        return this;
    }

    @Override
    public void onRequestSuccess(List<Address> addresses) {
        ToastUtils.showToast(this, "保存成功！");
        finish();
    }

    @Override
    public void onRequestNoData(String message) {

    }

    @Override
    public void onRequestError(String error_message) {

    }
}
